// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
// 引入路由 +挂载vue
import router from './router/router.js'

// 引入 element-ui
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'

// 引入公共的样式
import './assets/common.css'

// 解决axios的三个问题
import axios from 'axios'
// 问题1 : 基准路径
axios.defaults.baseURL = 'http://localhost:8888/api/private/v1/'
// 问题2 : 每次都要引入axios
Vue.prototype.$axios = axios // 每个组件都是vue实例,   this.$axios
// 问题3 : 请求拦截器
axios.interceptors.request.use(
  function (config) {
    config.headers.Authorization = localStorage.getItem('token')

    return config
  },
  function (error) {
    // Do something with request error
    return Promise.reject(error)
  }
)

// token 失效
// 响应拦截器
axios.interceptors.response.use(
  function (response) {
    // 判断token
    // if (response.data.meta.status === 10001) {
    //   this.$router.push('/login')
    // }

    // 判断是否有新的token字段
    if (response.data.token) {
      localStorage.setItem('token', response.data.token)
    }

    return response
  },
  function (error) {
    // Do something with response error
    return Promise.reject(error)
  }
)

// 使用use安装一下
Vue.use(ElementUI)

Vue.config.productionTip = false

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  components: { App },
  template: '<App/>'
})
